package com.pmcc.utils;

import org.springframework.data.domain.Page;

import java.util.List;

/**
 * Created by chuLiDong on 2018/9/16.
 */
public class ApiResultBean<T> {
    public static final String OUTLINE = "-1";//登录超时
    public static final String SUCCESS = "1";//查询成功
    public static final String FAIL = "0";//查询失败
    public static final String NO_RETURN = "2";//查询成功，返回结果为空
    public static final String NO_AUTHORITY = "3";//无权登录
    public static final String YES_AUTHORITY = "4";//可以登陆

    private String code; // 返回结果
    private String msg; // 返回结果描述
    private ApiData<T> data; // 返回的数据

    public ApiResultBean() {
        super();
        this.code = ApiResultBean.SUCCESS;
        this.msg = "操作成功！";
    }

    public ApiResultBean(String msg) {
        super();
        this.code = ApiResultBean.SUCCESS;
    }

    public ApiResultBean(String code, String msg) {
        super();
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public ApiData<T> getData() {
        return data;
    }

    public void setData(ApiData<T> data) {
        this.data = data;
    }

    public void setData(Page<T> pageContent, AntPage antPage) {
        if (this.data == null) {
            this.data = new ApiData<>();
        }
        this.data.setList(pageContent.getContent());
        this.data.setPage(antPage.getPage());
        this.data.setPageSize(antPage.getPageSize());
        this.data.setTotal(pageContent.getTotalElements());
    }

    public void setData(List<T> list, AntPage antPage) {
        if (this.data == null) {
            this.data = new ApiData<>();
        }
        this.data.setList(list);
        this.data.setPage(antPage.getPage());
        this.data.setPageSize(antPage.getPageSize());
    }

    public void setData(List<T> list, Long total) {
        if (this.data == null) {
            this.data = new ApiData<>();
        }
        this.data.setList(list);
        this.data.setTotal(total);
    }

    public void setData(List<T> list, AntPage antPage, Long total) {
        if (this.data == null) {
            this.data = new ApiData<>();
        }
        this.data.setList(list);
        this.data.setPage(antPage.getPage());
        this.data.setPageSize(antPage.getPageSize());
        this.data.setTotal(total);
    }

    public void setData(List<T> list) {
        if (this.data == null) {
            this.data = new ApiData<>();
        }
        this.data.setList(list);
    }

    public void setData(T object) {
        if (this.data == null) {
            this.data = new ApiData<>();
        }
        this.data.setObject(object);
    }
}
